package com.rbc.rlslips.properties.manager;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.rbc.rlslips.enums.CommonEnums;
import com.rbc.rlslips.enums.PropertiesKeysEnum;
import com.rbc.rlslips.logger.RLLogger;

public final class PropertiesReader {
	
	private static Logger logger = Logger.getLogger(PropertiesReader.class);
	
	private static Map<String,String> hashMap;
	
	private static Properties getPropertyFromIS(String fileName){
		RLLogger.debug(logger,"Entering initProperty method","fileName",fileName);
		InputStream inputStream = null;
	
		Properties prop = new Properties();
		try {
			inputStream = new  FileInputStream(new File(fileName));
			prop.load(inputStream);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			RLLogger.error(logger,"Exiting initProperty method Exception:",e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			RLLogger.error(logger,"Exiting initProperty method Exception:",e.getMessage());
			e.printStackTrace();
		}finally{
			if(inputStream != null){
				try {
					inputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					RLLogger.error(logger,"Exiting initProperty method finally block Exception:",e.getMessage());
					e.printStackTrace();
				}
			}
		}
		RLLogger.debug(logger,"Exiting initProperty method",prop);
		return prop;
		
	}
	
	public static String getProperty(PropertiesKeysEnum key)
	{
		RLLogger.debug(logger,"Entering getProperty method","fileName","Key",key.getEnumVal());
		String fileName = CommonEnums.RL_PROPERTIES_PATH.getEnumVal();
		String value = "";
		if(hashMap == null){
			hashMap = new HashMap<String, String>();
		}
		if(!hashMap.containsKey(key)){
			Properties properties = getPropertyFromIS(fileName);
			value = properties.getProperty(key.getEnumVal());
			hashMap.put(key.getEnumVal(),value);
		}else{
			value = hashMap.get(key);
		}
		RLLogger.debug(logger,"Exiting getProperty method","value",value);
		return value;
	}
}
